<?php
/**
 * app发送重置密码邮箱
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/5/28 14:52
 * @copyright PanxSoft Inc.
 */

namespace App\Services\Mailer\Customer;

use App\Exceptions\ApiErrCode;
use App\Exceptions\ApiException;
use App\Models\Setting;
use Illuminate\Mail\Mailer;
use Illuminate\Mail\Message;

class PasswordResetMail
{
    const RESET_PASSWORD_VIEW = 'email.admin-reset-password';


    /**
     * @param string $email
     * @param $new_pwd
     * @throws \Exception
     */
    public static function send(string $email, $new_pwd): void
    {
        /** @var Mailer $mailer */

        try{
            $mailer = app('mailer');

            $mailer->send(self::RESET_PASSWORD_VIEW, compact('new_pwd'),
                function (Message $msg) use ($email) {
                    $msg->subject(trans('service.reset_password',['tip' => Setting::configNameValue()]) . now());
                    /**
                     * 此处 from address 如果使用 qq 邮箱必须与 .env 中 MAIL_USERNAME 一致
                     */
                    $msg->from(config('mail.username'), Setting::configNameValue().'Reset Password');
                    $msg->to($email);
                });
        } catch (\Exception $exception) {
            throw new ApiException(ApiErrCode::EMAIL_SEND_FAILED,$exception->getMessage());
        }
    }
}